package com.ruicar.afs.cloud.apply.pre.loan.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.dto.ChannelInfoDTO;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCarDetails;
import com.ruicar.afs.cloud.apply.common.feign.Apply2ChannelFeign;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.loan.condition.ApplyReportCondition;
import com.ruicar.afs.cloud.apply.pre.loan.condition.CarInfoCondition;
import com.ruicar.afs.cloud.apply.pre.loan.mapper.ApplyCarDetailsMapper;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCarDetailsService;
import com.ruicar.afs.cloud.apply.pre.loan.vo.CarInfoVO;
import com.ruicar.afs.cloud.channel.common.dto.CarAllListDTO;
import com.ruicar.afs.cloud.channel.common.dto.CarInfoListDTO;
import com.ruicar.afs.cloud.channel.common.feign.ListBaseFeign;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.parameter.commom.condition.VehicleCondition;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleBrand;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleDetail;
import com.ruicar.afs.cloud.parameter.commom.enums.CarNature;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleDetailService;
import com.ruicar.afs.cloud.parameter.commom.vo.VehicleBrandVO;
import com.ruicar.afs.cloud.parameter.commom.vo.VehicleModelVO;
import com.ruicar.afs.cloud.vehicle.dto.CarInfoDto;
import com.ruicar.afs.cloud.vehicle.feign.VehicleApiFeign;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 业务选择-业务参数、资产信息、挂靠信息
 * @author: sijun.yu
 * @date: 2020/5/11 15:08
 */
@Service
@AllArgsConstructor
public class ApplyCarDetailsServiceImpl extends ServiceImpl<ApplyCarDetailsMapper, ApplyCarDetails> implements ApplyCarDetailsService {

    private VehicleApiFeign vehicleApiFeign;
    private TsysVehicleDetailService vehicleDetailService;
    private ListBaseFeign nameLibFeign;
    private Apply2ChannelFeign channelFeign;
    private ApplyConfig config;

    /**
     * 查询名单库-挂牌车
     *
     * @param vin
     * @return
     */
    public IResponse carListingSelecte(String vin) {

        CarInfoListDTO carToListCondition = new CarInfoListDTO();
        carToListCondition.setPageNum(ApplyConstants.PAGE_NUM);
        carToListCondition.setPageSize(ApplyConstants.PAGE_SIZE);
        carToListCondition.setStatus(ApplyConstants.STATUS_VALID);// 有效
        carToListCondition.setVinNumber(vin);
        carToListCondition.setPartnerId(SecurityUtils.getUser().getUserExtInfo().getString("channelId"));

        CarAllListDTO carVo = new CarAllListDTO();
        carVo.setData(carToListCondition);
        carVo.setType("tb_car_info");
        JSONObject jsonObject = null;
        try {
            jsonObject = nameLibFeign.carListingSelecte(carVo);
        } catch (Exception e) {
            return IResponse.fail("名单库系统异常，请联系管理员");
        }
        // 成功之后返回
        if (jsonObject.getInteger("code") == 200) {
            Assert.isTrue(jsonObject.getJSONObject("data").getInteger("total") > 0, "未关联到已备案的挂牌车，请重新输入VIN号");
            return IResponse.success(jsonObject.getJSONObject("data"));
        }
        return IResponse.fail(jsonObject.getString("msg"));
    }

    /**
     * 二手车进件接口
     *
     * @param vin
     * @return
     */
    public IResponse usedCar(String vin) {

        CarInfoDto dto = new CarInfoDto();
        dto.setVin(vin);
        try {
            JSONObject json = vehicleApiFeign.usedCar(dto);
            if (0 == json.getInteger("code")) {
                return IResponse.success(json.getJSONArray("data"));
            } else {
                return IResponse.fail(json.getString("msg"));
            }
        } catch (Exception e) {
            return IResponse.fail("车型库系统异常，请联系管理员");
        }
    }

    /**
     * 系统车型库、资产及码表同步参数接口
     *
     * @param version
     * @return
     */
    public JSONObject brandAndCode(Long version) {

        CarInfoDto dto = new CarInfoDto();
        dto.setVersion(version);
        return vehicleApiFeign.brandAndCode(dto);
    }

    /**
     * 车E估估值报告
     *
     * @param carInfoDto
     * @return
     */
    @Override
    public JSONObject getValuationReport(CarInfoDto carInfoDto) {

        JSONObject json = vehicleApiFeign.pgbgjson(carInfoDto);
        return json;
    }

    /**
     * 通过ID获取车辆信息
     *
     * @param id
     * @return
     */
    public ApplyCarDetails getCarDetailsById(Long id) {

        ApplyCarDetails applyCarDetails = this.baseMapper.selectOne(Wrappers.<ApplyCarDetails>query().lambda().eq(ApplyCarDetails::getId, id));
        return applyCarDetails;
    }

    /**
     * 通过申请编号获取车辆信息
     *
     * @param applyNo
     * @return
     */
    public ApplyCarDetails getCarDetailsByApplyNo(String applyNo) {

        ApplyCarDetails applyCarDetails = this.baseMapper.selectOne(Wrappers.<ApplyCarDetails>query().lambda().eq(ApplyCarDetails::getApplyNo, applyNo));
        return applyCarDetails;
    }

    /**
     * 获取车辆信息
     *
     * @param vo
     * @param condition
     * @return
     */
    public CarInfoVO getCarInfo(CarInfoVO vo, ApplyReportCondition condition) {

        ApplyCarDetails carDetails = this.getCarDetailsByApplyNo(condition.getApplyNo());
        if (carDetails != null) {
            vo = new CarInfoVO();
            vo.setApplyNo(carDetails.getApplyNo());
            if (CarNature.NEW_CAR.getIndex().equals(condition.getCarNature())) {
                ApplyCarDetails newCarInfo = new ApplyCarDetails();
                newCarInfo.setId(carDetails.getId());
                newCarInfo.setBrandId(carDetails.getBrandId());
                newCarInfo.setBrandName(carDetails.getBrandName());
                newCarInfo.setModelId(carDetails.getModelId());
                newCarInfo.setModelName(carDetails.getModelName());
                newCarInfo.setStyleId(carDetails.getStyleId());
                newCarInfo.setStyleName(carDetails.getStyleName());
                newCarInfo.setGuidePrice(carDetails.getGuidePrice());
                newCarInfo.setSalePrice(carDetails.getSalePrice());
                newCarInfo.setPurchaseProvince(carDetails.getPurchaseProvince());
                newCarInfo.setPurchaseCity(carDetails.getPurchaseCity());
                newCarInfo.setLicenseProvince(carDetails.getLicenseProvince());
                newCarInfo.setLicenseCity(carDetails.getLicenseCity());
                vo.setNewCarInfo(newCarInfo);
            } else if (CarNature.PLATE_CAR.getIndex().equals(condition.getCarNature())) {
                ApplyCarDetails licenseCarInfo = new ApplyCarDetails();
                licenseCarInfo.setId(carDetails.getId());
                licenseCarInfo.setCarVin(carDetails.getCarVin());
                licenseCarInfo.setBrandId(carDetails.getBrandId());
                licenseCarInfo.setBrandName(carDetails.getBrandName());
                licenseCarInfo.setModelId(carDetails.getModelId());
                licenseCarInfo.setModelName(carDetails.getModelName());
                licenseCarInfo.setStyleId(carDetails.getStyleId());
                licenseCarInfo.setStyleName(carDetails.getStyleName());
                licenseCarInfo.setGuidePrice(carDetails.getGuidePrice());
                licenseCarInfo.setSalePrice(carDetails.getSalePrice());
                licenseCarInfo.setCarFunction(carDetails.getCarFunction());
                licenseCarInfo.setEngineNo(carDetails.getEngineNo());
                licenseCarInfo.setPurchaseProvince(carDetails.getPurchaseProvince());
                licenseCarInfo.setPurchaseCity(carDetails.getPurchaseCity());
                licenseCarInfo.setLicenseProvince(carDetails.getLicenseProvince());
                licenseCarInfo.setLicenseCity(carDetails.getLicenseCity());
                licenseCarInfo.setMileage(carDetails.getMileage());
                licenseCarInfo.setVehicleMadeDate(carDetails.getVehicleMadeDate());
                licenseCarInfo.setFirstLandingDate(carDetails.getFirstLandingDate());
                licenseCarInfo.setLicensePlate(carDetails.getLicensePlate());
                licenseCarInfo.setFirstLicenseProvince(carDetails.getFirstLicenseProvince());
                licenseCarInfo.setFirstLicenseCity(carDetails.getFirstLicenseCity());
                licenseCarInfo.setFirstLicenseAddress(carDetails.getFirstLicenseAddress());
                licenseCarInfo.setCarBelongs(carDetails.getCarBelongs());
                licenseCarInfo.setBelongsCertNo(carDetails.getBelongsCertNo());
                licenseCarInfo.setInvoiceTime(carDetails.getInvoiceTime());
                licenseCarInfo.setEvaluatingPrice(carDetails.getEvaluatingPrice());
                licenseCarInfo.setIsFirstLicense(carDetails.getIsFirstLicense());
                vo.setLicenseCarInfo(licenseCarInfo);
            } else {
                ApplyCarDetails oldCarInfo = new ApplyCarDetails();
                BeanUtils.copyProperties(carDetails, oldCarInfo);
                vo.setOldCarInfo(oldCarInfo);
            }
        }
        return vo;
    }

    /**
     * 通过vin获取数量
     *
     * @param carVin
     * @return
     */
    public Integer getCarVinCount(String carVin) {
        // 传入空值返回为0
        if (StringUtils.isBlank(carVin)) {
            return 0;
        }
        return this.baseMapper.getCarVinCount(carVin);
    }

    /**
     * 保存车辆信息
     *
     * @param condition
     * @return
     */
    public ApplyCarDetails saveCarInfo(String applyNo, CarInfoCondition condition) {

        ApplyCarDetails carDetails = new ApplyCarDetails();
        if (CarNature.NEW_CAR.getIndex().equals(condition.getCarNature())) {
            carDetails.setBrandId(condition.getBrandId());
            carDetails.setBrandName(condition.getBrandName());
            carDetails.setModelId(condition.getModelId());
            carDetails.setModelName(condition.getModelName());
            carDetails.setStyleId(condition.getStyleId());
            carDetails.setStyleName(condition.getStyleName());
            carDetails.setGuidePrice(condition.getGuidePrice());
            carDetails.setSalePrice(condition.getSalePrice());
            carDetails.setPurchaseProvince(condition.getPurchaseProvince());
            carDetails.setPurchaseCity(condition.getPurchaseCity());
            carDetails.setLicenseProvince(condition.getLicenseProvince());
            carDetails.setLicenseCity(condition.getLicenseCity());
            // 获取车型库完整信息
            TsysVehicleDetail vehicleDetail = vehicleDetailService.getVehicleDetail(condition.getStyleId(), condition.getModelId());
            if (vehicleDetail != null) {
                carDetails.setIsInternet(vehicleDetail.getIsInternet());
                carDetails.setIsGreen(vehicleDetail.getIsGreen());
                carDetails.setSeatNum(vehicleDetail.getSeatNum());
                carDetails.setConfigure(vehicleDetail.getConfigure());
                carDetails.setYearStyle(vehicleDetail.getYearStyle());
                carDetails.setDriver(vehicleDetail.getDriver());
                carDetails.setBlowoff(vehicleDetail.getBlowoff());
                carDetails.setCarTypeMain(vehicleDetail.getCarType());
                carDetails.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                carDetails.setCarBodyClass(vehicleDetail.getCarLevel());
                carDetails.setCarTransmission(vehicleDetail.getTransmission());
                carDetails.setCarDisplacement(vehicleDetail.getDisplacement());
            }
        } else if (CarNature.PLATE_CAR.getIndex().equals(condition.getCarNature())) {
            carDetails.setCarVin(condition.getCarVin());
            carDetails.setBrandId(condition.getBrandId());
            carDetails.setBrandName(condition.getBrandName());
            carDetails.setModelId(condition.getModelId());
            carDetails.setModelName(condition.getModelName());
            carDetails.setStyleId(condition.getStyleId());
            carDetails.setStyleName(condition.getStyleName());
            carDetails.setGuidePrice(condition.getGuidePrice());
            carDetails.setSalePrice(condition.getSalePrice());
            carDetails.setCarFunction(condition.getCarFunction());
            carDetails.setEngineNo(condition.getEngineNo());
            carDetails.setPurchaseProvince(condition.getPurchaseProvince());
            carDetails.setPurchaseCity(condition.getPurchaseCity());
            carDetails.setLicenseProvince(condition.getLicenseProvince());
            carDetails.setLicenseCity(condition.getLicenseCity());
            carDetails.setMileage(condition.getMileage());
            if (condition.getVehicleMadeDate() != null) {
                carDetails.setVehicleMadeDate(condition.getVehicleMadeDate());
            }
            if (condition.getFirstLandingDate() != null) {
                carDetails.setFirstLandingDate(condition.getFirstLandingDate());
            }
            carDetails.setLicensePlate(condition.getLicensePlate());
            carDetails.setFirstLicenseProvince(condition.getFirstLicenseProvince());
            carDetails.setFirstLicenseCity(condition.getFirstLicenseCity());
            carDetails.setFirstLicenseAddress(condition.getFirstLicenseAddress());
            carDetails.setCarBelongs(condition.getCarBelongs());
            carDetails.setBelongsCertNo(condition.getBelongsCertNo());
            carDetails.setInvoiceTime(condition.getInvoiceTime());
            carDetails.setEvaluatingPrice(condition.getEvaluatingPrice());// 评估价格
            carDetails.setIsFirstLicense(condition.getIsFirstLicense());// 是否上次上牌
            carDetails.setTransNums(Integer.valueOf(0));// 总过户次数，默认为0
            // 获取车型库完整信息
            TsysVehicleDetail vehicleDetail = vehicleDetailService.getVehicleDetail(condition.getStyleId(), condition.getModelId());
            if (vehicleDetail != null) {
                carDetails.setIsInternet(vehicleDetail.getIsInternet());
                carDetails.setIsGreen(vehicleDetail.getIsGreen());
                carDetails.setSeatNum(vehicleDetail.getSeatNum());
                carDetails.setConfigure(vehicleDetail.getConfigure());
                carDetails.setYearStyle(vehicleDetail.getYearStyle());
                carDetails.setDriver(vehicleDetail.getDriver());
                carDetails.setBlowoff(vehicleDetail.getBlowoff());
                carDetails.setCarTypeMain(vehicleDetail.getCarType());
                carDetails.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                carDetails.setCarBodyClass(vehicleDetail.getCarLevel());
                carDetails.setCarTransmission(vehicleDetail.getTransmission());
                carDetails.setCarDisplacement(vehicleDetail.getDisplacement());
            }
        } else {
            BeanUtils.copyProperties(condition, carDetails);
            carDetails.setSalePrice(condition.getSecondSalePrice());// 二手车价格
            if (condition.getFirstLandingDate() != null) {
                Long days = DateUtil.between(DateUtil.date(), condition.getFirstLandingDate(), DateUnit.DAY);
                BigDecimal vehicleAge = new BigDecimal(days).divide(new BigDecimal("360"), 2, BigDecimal.ROUND_HALF_DOWN);
                carDetails.setVehicleAge(vehicleAge.toString());// 车龄
            }
            carDetails.setBrandId("");// 品牌ID
            carDetails.setModelId("");// 款式ID
            carDetails.setStyleId("");// 车型ID
        }
        carDetails.setApplyNo(applyNo);
        this.baseMapper.insert(carDetails);

        return carDetails;
    }


    /**
     * 更新车辆信息
     *
     * @param carDetails
     * @param condition
     */
    public void updateCarInfo(ApplyCarDetails carDetails, CarInfoCondition condition) {

        if (CarNature.NEW_CAR.getIndex().equals(condition.getCarNature())) {
            carDetails.setCarVin("");// 车架号
            carDetails.setFullModelNum("");// 车辆型号
            carDetails.setEngineNo("");// 发动机号
            carDetails.setBrandId(condition.getBrandId());
            carDetails.setBrandName(condition.getBrandName());
            carDetails.setModelId(condition.getModelId());
            carDetails.setModelName(condition.getModelName());
            carDetails.setStyleId(condition.getStyleId());
            carDetails.setStyleName(condition.getStyleName());
            carDetails.setGuidePrice(condition.getGuidePrice());
            carDetails.setSalePrice(condition.getSalePrice());
            carDetails.setPurchaseProvince(condition.getPurchaseProvince());
            carDetails.setPurchaseCity(condition.getPurchaseCity());
            carDetails.setLicenseProvince(condition.getLicenseProvince());
            carDetails.setLicenseCity(condition.getLicenseCity());
            // 获取车型库完整信息
            TsysVehicleDetail vehicleDetail = vehicleDetailService.getVehicleDetail(condition.getStyleId(), condition.getModelId());
            if (vehicleDetail != null) {
                carDetails.setIsInternet(vehicleDetail.getIsInternet());
                carDetails.setIsGreen(vehicleDetail.getIsGreen());
                carDetails.setSeatNum(vehicleDetail.getSeatNum());
                carDetails.setConfigure(vehicleDetail.getConfigure());
                carDetails.setYearStyle(vehicleDetail.getYearStyle());
                carDetails.setDriver(vehicleDetail.getDriver());
                carDetails.setBlowoff(vehicleDetail.getBlowoff());
                carDetails.setCarTypeMain(vehicleDetail.getCarType());
                carDetails.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                carDetails.setCarBodyClass(vehicleDetail.getCarLevel());
                carDetails.setCarTransmission(vehicleDetail.getTransmission());
                carDetails.setCarDisplacement(vehicleDetail.getDisplacement());
            }
        } else if (CarNature.PLATE_CAR.getIndex().equals(condition.getCarNature())) {
            carDetails.setCarVin(condition.getCarVin());
            carDetails.setBrandId(condition.getBrandId());
            carDetails.setBrandName(condition.getBrandName());
            carDetails.setModelId(condition.getModelId());
            carDetails.setModelName(condition.getModelName());
            carDetails.setStyleId(condition.getStyleId());
            carDetails.setStyleName(condition.getStyleName());
            carDetails.setGuidePrice(condition.getGuidePrice());
            carDetails.setSalePrice(condition.getSalePrice());
            carDetails.setCarFunction(condition.getCarFunction());
            carDetails.setEngineNo(condition.getEngineNo());
            carDetails.setPurchaseProvince(condition.getPurchaseProvince());
            carDetails.setPurchaseCity(condition.getPurchaseCity());
            carDetails.setLicenseProvince(condition.getLicenseProvince());
            carDetails.setLicenseCity(condition.getLicenseCity());
            carDetails.setMileage(condition.getMileage());
            if (condition.getVehicleMadeDate() != null) {
                carDetails.setVehicleMadeDate(condition.getVehicleMadeDate());
            }
            if (condition.getFirstLandingDate() != null) {
                carDetails.setFirstLandingDate(condition.getFirstLandingDate());
            }
            carDetails.setLicensePlate(condition.getLicensePlate());
            carDetails.setFirstLicenseProvince(condition.getFirstLicenseProvince());
            carDetails.setFirstLicenseCity(condition.getFirstLicenseCity());
            carDetails.setFirstLicenseAddress(condition.getFirstLicenseAddress());
            carDetails.setCarBelongs(condition.getCarBelongs());
            carDetails.setBelongsCertNo(condition.getBelongsCertNo());
            carDetails.setInvoiceTime(condition.getInvoiceTime());
            carDetails.setEvaluatingPrice(condition.getEvaluatingPrice());// 评估价格
            carDetails.setIsFirstLicense(condition.getIsFirstLicense());// 是否上次上牌
            carDetails.setTransNums(Integer.valueOf(0));// 总过户次数，默认为0
            // 获取车型库完整信息
            TsysVehicleDetail vehicleDetail = vehicleDetailService.getVehicleDetail(condition.getStyleId(), condition.getModelId());
            if (vehicleDetail != null) {
                carDetails.setIsInternet(vehicleDetail.getIsInternet());
                carDetails.setIsGreen(vehicleDetail.getIsGreen());
                carDetails.setSeatNum(vehicleDetail.getSeatNum());
                carDetails.setConfigure(vehicleDetail.getConfigure());
                carDetails.setYearStyle(vehicleDetail.getYearStyle());
                carDetails.setDriver(vehicleDetail.getDriver());
                carDetails.setBlowoff(vehicleDetail.getBlowoff());
                carDetails.setCarTypeMain(vehicleDetail.getCarType());
                carDetails.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                carDetails.setCarBodyClass(vehicleDetail.getCarLevel());
                carDetails.setCarTransmission(vehicleDetail.getTransmission());
                carDetails.setCarDisplacement(vehicleDetail.getDisplacement());
            }
        } else {
            BeanUtils.copyProperties(condition, carDetails);
            carDetails.setSalePrice(condition.getSecondSalePrice());// 二手车价格
            if (condition.getFirstLandingDate() != null) {
                Long days = DateUtil.between(DateUtil.date(), condition.getFirstLandingDate(), DateUnit.DAY);
                BigDecimal vehicleAge = new BigDecimal(days).divide(new BigDecimal("360"), 2, BigDecimal.ROUND_HALF_DOWN);
                carDetails.setVehicleAge(vehicleAge.toString());// 车龄
            }
            carDetails.setBrandId("");// 品牌ID
            carDetails.setModelId("");// 款式ID
            carDetails.setStyleId("");// 车型ID
        }
        this.baseMapper.updateById(carDetails);
    }

    /**
     * 获取产品请求头
     *
     * @return
     */
    public Map makeCaseHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", config.getCaseClientId());
        headers.put("clientSecret", config.getCaseClientSecret());
        return headers;
    }

    /**
     * 查询品牌列表
     *
     * @param condition
     * @return
     */
    public IResponse getBrandList(VehicleCondition condition) {

        // 查询渠道管理品牌列表
        String channelId = SecurityUtils.getUser().getUserExtInfo().getJSONObject("channelInfo").getString("channelId");
        condition.setChannelId(Long.valueOf(channelId));
        IResponse<JSONObject> vehicleJson = channelFeign.getBrandList(condition, makeCaseHeader());
        String flag = vehicleJson.getData().getString("authVehicleTypeSwitch");
        // 取自己车型库
        if (WhetherEnum.No.getIndex().equals(flag)) {
            List<TsysVehicleBrand> list = vehicleDetailService.getVehicleBrandList(condition);
            List<VehicleBrandVO> resultList = new ArrayList<VehicleBrandVO>();
            for (TsysVehicleBrand brand : list) {
                VehicleBrandVO vo = new VehicleBrandVO();
                vo.setBrandId(brand.getCode());
                vo.setBrandName(brand.getName());
                Character firstChar = Character.toUpperCase(brand.getSpellCode().charAt(0));
                vo.setFirstChar(firstChar);
                resultList.add(vo);
            }
            return IResponse.success(resultList);
        }

        return IResponse.success(vehicleJson.getData().getJSONArray("resultList"));
    }

    /**
     * 查询车型列表
     *
     * @param condition
     * @return
     */
    public IResponse getModelList(VehicleCondition condition) {

        String channelId = SecurityUtils.getUser().getUserExtInfo().getJSONObject("channelInfo").getString("channelId");
        condition.setChannelId(Long.valueOf(channelId));
        // 查询渠道关联的车型
        IResponse<JSONObject> vehicleJson = channelFeign.getModelList(condition, makeCaseHeader());
        String flag = vehicleJson.getData().getString("authVehicleTypeSwitch");
        // 取自己车型库
        if (WhetherEnum.No.getIndex().equals(flag) || "false".equals(condition.getIndeterminate())) {
            List<VehicleModelVO> resultList = vehicleDetailService.getModelList(condition);
            return IResponse.success(resultList);
        }
        return IResponse.success(vehicleJson.getData().getJSONArray("resultList"));
    }

    /**
     * 查询车型列表 - 模糊
     *
     * @param condition
     * @return
     */
    public IResponse getModelTitleList(VehicleCondition condition) {

        String channelId = SecurityUtils.getUser().getUserExtInfo().getJSONObject("channelInfo").getString("channelId");
        condition.setChannelId(Long.valueOf(channelId));
        // 查询渠道关联的车型
        IResponse<JSONObject> vehicleJson =  channelFeign.getModelTitleList(condition,makeCaseHeader());
        String flag = WhetherEnum.No.getIndex();
        if(vehicleJson.getData()!=null && vehicleJson.getData().containsValue("authVehicleTypeSwitch")){
            flag = vehicleJson.getData().getString("authVehicleTypeSwitch");
        }
        // 取自己车型库
        if (WhetherEnum.No.getIndex().equals(flag)) {
            List<VehicleModelVO> resultList = vehicleDetailService.getModelList(condition);
            return IResponse.success(resultList);
        }
        return IResponse.success(vehicleJson.getData().getJSONArray("resultList"));
    }

    /**
     * 获取车商省份和城市
     *
     * @param dto
     * @return
     */
    public IResponse getAuthorizeRegion(ChannelInfoDTO dto) {

        return channelFeign.getAuthorizeRegion(dto, this.makeCaseHeader());
    }

}
